package com.sise.bl.dao.impl;

import com.sise.bl.dao.AddressDao;
import com.sise.bl.entity.Address;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("addressDao")
@SuppressWarnings("all")
public class AddressDaoImpl extends BaseDaoImpl<Address> implements AddressDao {

    @Override
    public List<Address> selectAddressByUid(Integer uid) {
        String sql = "select a from Address a,User u where a.user.uid=u.uid and a.user.uid=:uid";
        Map<String ,Object> params = new HashMap<String,Object>();
        params.put("uid",uid);
        List<Address> addresses = find(sql, params);
        return addresses;
    }

    @Override
    public List<Address> selectStateDefault() {
        String sql = "from Address a where a.astate = 1";
        List<Address> objects = find(sql);
        return objects;
    }

    @Override
    public Address selectByAid(Integer aid) {
        return get(aid);
    }

    @Override
    public int insertAddress(Address address) {
        int save = (int) save(address);
        return save;
    }

    @Override
    public void deleteAddressByAid(Address address) {
        delete(address);
    }

    @Override
    public void deleteAddressByUid(String uid) {
        String sql = "delete from Address a where a.user.uid=?0";
        Query query = this.getCurrentSession().createQuery(sql);
        query.setParameter(0,Integer.valueOf(uid));
        query.executeUpdate();
    }

    @Override
    public void updateAddressByAid(Address address) {
        update(address);
    }

}
